package com.wst.system.interceptor.encrypt;

import com.wst.system.config.exception.GlobalExcetion;
import org.apache.commons.lang3.StringUtils;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.lang.reflect.Field;

public class EncryptAspectCommon {

    public static void dealParamValited(Object obj) throws GlobalExcetion {
        Field[] fields=obj.getClass().getDeclaredFields();
        StringBuilder stringBuilder=new StringBuilder();
        for(Field field:fields){
            field.setAccessible(true);
            NotNull notNull=field.getAnnotation(NotNull.class);
            NotBlank notBlank=field.getAnnotation(NotBlank.class);
            Object object=null;
            if(notNull!=null || notBlank!=null){
                try {
                    object=field.get(obj);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
                if(notNull!=null){
                    if(object==null){
                        stringBuilder.append(notNull.message());
                    }
                }
                if(notBlank!=null){
                    if(!StringUtils.isNotBlank(object.toString())){
                        stringBuilder.append(notBlank.message());
                    }
                }
            }
        }
        if(stringBuilder.length()>0){
            throw new GlobalExcetion(1,stringBuilder.toString());
        }
    }
}
